package com.zb.controller;

import com.zb.service.DeviceService;
import com.zb.util.Pager;
import com.zb.util.ResultResponse;
import com.zb.vo.DeviceQuotaVO;
import com.zb.vo.DeviceSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/device")
@CrossOrigin
public class DeviceController {

    @Autowired
    private DeviceService deviceService;


    @PostMapping("/device-online-status")
    public void deviceOnlineStatus(@RequestBody Map<String,Object> param){
        System.out.println(param);
        String deviceId = param.get("clientid").toString();
        //只处理设备的上下线操作，其他内容不处理
        if (deviceId.startsWith("web-server")){
            return;
        }
        String action = param.get("action").toString();
        if ("client_connected".equals(action)){
            deviceService.setOnline(deviceId,true);
        }else {
            deviceService.setOnline(deviceId,false);
        }
    }

    @PostMapping("/search")
    public ResultResponse search(@RequestBody DeviceSearchVO deviceSearchVO){
//        return deviceService.search(deviceSearchVO);
        Pager<DeviceQuotaVO> pager = deviceService.queryDeviceQuota(deviceSearchVO.getIndex(), deviceSearchVO.getSize(),
                deviceSearchVO.getDeviceId(), deviceSearchVO.getTag(), deviceSearchVO.getStatus());
        return ResultResponse.success(pager);
    }

    @PutMapping("/status")
    public ResultResponse setStatus(@RequestParam("deviceId")String deviceId,
                                    @RequestParam("status")Boolean status){
        deviceService.setStatus(deviceId,status);
        return ResultResponse.success();
    }
    @PutMapping("/tag")
    public ResultResponse setTag(@RequestParam("deviceId")String deviceId,
                                    @RequestParam("tag")String tag){
        deviceService.setTag(deviceId,tag);
        return ResultResponse.success();
    }

    @PutMapping("/online")
    public ResultResponse setOnline(@RequestParam("deviceId")String deviceId,
                                 @RequestParam("online")Boolean online){
        deviceService.setOnline(deviceId,online);
        return ResultResponse.success();
    }
}
